﻿Imports System.Data

Partial Class WebForm_AddWorkHeaderControl
    Inherits System.Web.UI.UserControl

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
        'Me.Controls.Add(Me.LoadControl("~/WebForm/CommonControl/GoogleMapExif.ascx"))
    End Sub


    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.FormView_WorkHeader.InsertItemTemplate = Me.FormView_WorkHeader.ItemTemplate
        Me.FormView_WorkHeader.EditItemTemplate = Me.FormView_WorkHeader.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.FormView_WorkHeader.FindControl("btnUploadPreExecutionPhoto") IsNot Nothing Then
            ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.FormView_WorkHeader.FindControl("btnUploadPreExecutionPhoto"))
            ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.FormView_WorkHeader.FindControl("btnUploadPostExecutionPhoto"))
        End If
    End Sub

    Protected Sub rdoButifyTreeCountUnitID_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dt As DataTable = MyObj.GetCodeMappingData("CleanPlan", "ButifyTreeCountUnit")

        Dim rdoButifyTreeCountUnitID As RadioButtonList = TryCast(sender, RadioButtonList)

        If rdoButifyTreeCountUnitID.Items.Count = 0 AndAlso (Not dt Is Nothing) AndAlso (dt.Rows.Count > 0) Then

            For Each objDataRow As DataRow In dt.Rows
                rdoButifyTreeCountUnitID.Items.Add(New ListItem(objDataRow.Item("Text").ToString(), objDataRow.Item("Value").ToString()))
            Next

        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        'TryCast(FormView_WorkHeader.FindControl("chk_txtStartDate"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtStartDate2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtEndDate"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtEndDate2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtEndDate3"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtPeopleJoined"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtPeopleJoined2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanTimes"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanTimes2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanToiletCount"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanToiletCount2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanAdvCount"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtCleanAdvCount2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtButifyLocationCount"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtButifyLocationCount2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtButifyTreeCount"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtButifyTreeCount2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_rdoButifyTreeCountUnitID"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtReportDirtyTImes"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup
        'TryCast(FormView_WorkHeader.FindControl("chk_txtReportDirtyTImes2"), System.Web.UI.WebControls.BaseValidator).ValidationGroup = ValidationGroup

    End Sub

    Protected Sub FormView_WorkHeader_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView_WorkHeader.DataBound
        Me.BindCleanPeopleType()

        If Me.FormView_WorkHeader.CurrentMode = FormViewMode.Edit Then
            Me.UOrganizationID = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "UOrganizationID").ToString()
            Me.VOrganizationID = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "VUOrganizationID").ToString()
            Me.VolunteerID = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "LeaderID").ToString()
            Me.Approved = IIf(DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "Active").ToString() = "Y", True, False)

            '繫结開始時間
            If Not DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "TimeStart").ToString() = "0" Then
                TryCast(Me.FormView_WorkHeader.FindControl("SelectTimeStart"), WebForm_ReserveCar_SelectTime).Text = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "TimeStart").ToString()
            Else
                TryCast(Me.FormView_WorkHeader.FindControl("SelectTimeStart"), WebForm_ReserveCar_SelectTime).Text = "24"
            End If

            '繫结結束時間
            If Not DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "TimeEnd").ToString() = "0" Then
                TryCast(Me.FormView_WorkHeader.FindControl("SelectTimeEnd"), WebForm_ReserveCar_SelectTime).Text = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "TimeEnd").ToString()
            Else
                TryCast(Me.FormView_WorkHeader.FindControl("SelectTimeEnd"), WebForm_ReserveCar_SelectTime).Text = "24"
            End If

            '繫结清潔方式
            Dim rbList As RadioButtonList = TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList)
            Dim IsOther As Boolean = True
            For i As Integer = 0 To rbList.Items.Count - 1
                If DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "CleanPeopleType").ToString() = rbList.Items(i).Text Then
                    rbList.Items(i).Selected = True
                    IsOther = False
                    Exit For
                End If
            Next

            If IsOther Then
                rbList.Items(2).Selected = True
                TryCast(Me.FormView_WorkHeader.FindControl("txtOther"), TextBox).Text = DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "CleanPeopleType").ToString()
            End If

            '繫結GoogleMap
            Dim filePath As String = Server.MapPath(Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PreExecutionPhotoName").ToString())
            If System.IO.File.Exists(filePath) Then
                TryCast(Me.FormView_WorkHeader.FindControl("GoogleMapExif1"), WebForm_CommonControl_GoogleMapExif).PicFilePath = Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PreExecutionPhotoName").ToString()
            End If
            filePath = Server.MapPath(Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PostExecutionPhotoName").ToString())
            If System.IO.File.Exists(filePath) Then
                TryCast(Me.FormView_WorkHeader.FindControl("GoogleMapExif2"), WebForm_CommonControl_GoogleMapExif).PicFilePath = Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PostExecutionPhotoName").ToString()
            End If

            'TryCast(Me.FormView_WorkHeader.FindControl("GoogleMapExif1"), WebForm_CommonControl_GoogleMapExif).PicFilePath = Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PreExecutionPhotoName").ToString()
            'TryCast(Me.FormView_WorkHeader.FindControl("GoogleMapExif2"), WebForm_CommonControl_GoogleMapExif).PicFilePath = Me.PhotoPathBase & DataBinder.Eval(Me.FormView_WorkHeader.DataItem, "PostExecutionPhotoName").ToString()

        ElseIf Me.FormView_WorkHeader.CurrentMode = FormViewMode.Insert Then
            ''隱藏清潔內容
            'TryCast(Me.FormView_WorkHeader.FindControl("PanelA"), Panel).Visible = False
            'TryCast(Me.FormView_WorkHeader.FindControl("AddWorkItemControl1"), WebForm_AddControl_AddWorkItemControl).Visible = False
        End If
        If Me.FormView_WorkHeader.FindControl("btnUploadPreExecutionPhoto") IsNot Nothing Then
            ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.FormView_WorkHeader.FindControl("btnUploadPreExecutionPhoto"))
            ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.FormView_WorkHeader.FindControl("btnUploadPostExecutionPhoto"))
        End If
    End Sub

    Protected Sub FormView_WorkHeader_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs)
        If e.CommandName = "GetData" Then
            Me.SearchUOrganizationControl1.Show(Me.UOrganizationID)
        End If
    End Sub

    Protected Sub SearchUOrganizationControl1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchUOrganizationControl1.SelectedIndexChanged
        TryCast(Me.FormView_WorkHeader.FindControl("txtUOrganizationName"), TextBox).Text = Me.SearchUOrganizationControl1.OrganizationName
        TryCast(Me.FormView_WorkHeader.FindControl("txtVOrganizationName"), TextBox).Text = Me.SearchUOrganizationControl1.VOrganizationName
        TryCast(Me.FormView_WorkHeader.FindControl("txtLeaderName"), TextBox).Text = Me.SearchUOrganizationControl1.VolunteerName
        TryCast(Me.FormView_WorkHeader.FindControl("txtLeaderTel"), TextBox).Text = Me.SearchUOrganizationControl1.OfficePhone

        Me.UOrganizationID = Me.SearchUOrganizationControl1.UOrganizationID
        Me.VOrganizationID = Me.SearchUOrganizationControl1.VOrganizationID
        Me.VolunteerID = Me.SearchUOrganizationControl1.VolunteerID
    End Sub

    Protected Sub FormView_WorkHeader_ItemCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView_WorkHeader.ItemCommand
        If e.CommandName = "UploadPostExecutionPhoto" Then
            Me.SavePostPhoto()
        ElseIf e.CommandName = "UploadPreExecutionPhoto" Then
            Me.SavePrePhoto()
        End If
    End Sub
End Class

Partial Class WebForm_AddWorkHeaderControl

    Public Sub InitData(ByVal intWorkHeaderID As Integer)

        WorkHeaderID = intWorkHeaderID

        If intWorkHeaderID = 0 Then
            '新增
            Me.DataAccessMode = FormViewMode.Insert
            Me.FormView_WorkHeader.ChangeMode(FormViewMode.Insert)
        Else
            '編輯
            Me.DataAccessMode = FormViewMode.Edit
            Me.FormView_WorkHeader.ChangeMode(FormViewMode.Edit)

        End If

        ObjectDataSource1.SelectParameters("WorkHeaderID").DefaultValue = WorkHeaderID
        Me.FormView_WorkHeader.DataBind()

    End Sub

    Protected Sub BindCleanPeopleType()
        If TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList) IsNot Nothing Then
            TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList).DataTextField = "Text"
            TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList).DataValueField = "Value"
            TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList).DataSource = MyObj.GetCodeMappingData("WorkHeader", "CleanPeopleType")
            TryCast(Me.FormView_WorkHeader.FindControl("rbListCleanPeopleType"), RadioButtonList).DataBind()
        End If
    End Sub

#Region "Property"

    Public Property DataAccessMode() As FormViewMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As FormViewMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property

    Public Property PlanID() As Integer
        Get
            Return ViewState("PlanID")
        End Get
        Set(ByVal value As Integer)
            ViewState("PlanID") = value
        End Set

    End Property

    Public Property WorkHeaderID() As Integer
        Get
            Return ViewState("WorkHeaderID")
        End Get
        Set(ByVal value As Integer)
            ViewState("WorkHeaderID") = value
        End Set

    End Property

    Public Property ValidationGroup() As String
        Get
            Return ViewState("ValidationGroup")
        End Get
        Set(ByVal value As String)
            ViewState("ValidationGroup") = value
        End Set

    End Property

    Public Property UOrganizationID() As String
        Get
            Return ViewState("UOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("UOrganizationID") = value
        End Set

    End Property

    Public Property VOrganizationID() As String
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set

    End Property

    Public Property VolunteerID() As String
        Get
            Return ViewState("VolunteerID")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerID") = value
        End Set

    End Property

    Public Property Approved() As Boolean
        Get
            Return ViewState("Approved")
        End Get
        Set(ByVal value As Boolean)
            ViewState("Approved") = value
        End Set

    End Property

    Public ReadOnly Property PreExecutionPhotoName() As String
        Get
            Dim fu As FileUpload = TryCast(Me.FormView_WorkHeader.FindControl("fuPreExecutionPhoto"), FileUpload)

            If fu.HasFile = True Then
                Return String.Format("{0}a{1}", Me.WorkHeaderID, IO.Path.GetExtension(fu.FileName))
            Else
                Return ""
            End If
        End Get
    End Property

    Public ReadOnly Property PostExecutionPhotoName() As String
        Get
            Dim fu As FileUpload = TryCast(Me.FormView_WorkHeader.FindControl("fuPostExecutionPhoto"), FileUpload)

            If fu.HasFile = True Then
                Return String.Format("{0}b{1}", Me.WorkHeaderID, IO.Path.GetExtension(fu.FileName))
            Else
                Return ""
            End If
        End Get
    End Property

    Public Sub SavePhoto()
        'Dim fu As FileUpload

        ''處理前照片
        'fu = TryCast(Me.FormView_WorkHeader.FindControl("fuPreExecutionPhoto"), FileUpload)

        'If fu.HasFile <> False Then
        '    fu.SaveAs(Server.MapPath(Me.PhotoPathBase + Me.PreExecutionPhotoName))
        'End If

        ''處理後照片
        'fu = TryCast(Me.FormView_WorkHeader.FindControl("fuPostExecutionPhoto"), FileUpload)

        'If fu.HasFile <> False Then
        '    fu.SaveAs(Server.MapPath(Me.PhotoPathBase + Me.PostExecutionPhotoName))
        'End If

    End Sub

    Protected Sub SavePrePhoto()
        Dim fu As FileUpload
        fu = TryCast(Me.FormView_WorkHeader.FindControl("fuPreExecutionPhoto"), FileUpload)

        If Not fu.HasFile Then
            MyObj.ShowMessage("請先選擇檔案!!")
        Else
            '處理前照片
            Dim dat As New clsData
            Dim Sql As New MyStringBuilder

            Sql.AppendFormat("UPDATE WorkHeader set ")

            Sql.AppendFormat("PreExecutionPhotoName = '{0}' ", Me.PreExecutionPhotoName)

            Sql.AppendFormat("WHERE WorkHeaderID = '{0}';", Me.WorkHeaderID)

            dat.AccData(Sql)


            fu.SaveAs(Server.MapPath("~/Downloads/WorkRecordPhoto/" + Me.PreExecutionPhotoName))

            Me.InitData(Me.WorkHeaderID)
        End If
    End Sub

    Protected Sub SavePostPhoto()

        Dim fu As FileUpload
        fu = TryCast(Me.FormView_WorkHeader.FindControl("fuPostExecutionPhoto"), FileUpload)

        If Not fu.HasFile Then
            MyObj.ShowMessage("請先選擇檔案!!")
        Else
            '處理後照片
            Dim dat As New clsData
            Dim Sql As New MyStringBuilder

            Sql.AppendFormat(" UPDATE WorkHeader set ")

            Sql.AppendFormat(" PostExecutionPhotoName = '{0}' ", Me.PostExecutionPhotoName)

            Sql.AppendFormat(" WHERE WorkHeaderID = '{0}';", Me.WorkHeaderID)

            dat.AccData(Sql)

            '處理後照片
            fu.SaveAs(Server.MapPath("~/Downloads/WorkRecordPhoto/" + Me.PostExecutionPhotoName))

            Me.InitData(Me.WorkHeaderID)
        End If

    End Sub

    Protected ReadOnly Property PhotoPathBase() As String
        Get
            If ViewState("PhotoPathBase") Is Nothing Then
                ViewState("PhotoPathBase") = MyObj.GetFilePath("WorkRecordPhoto")
            End If

            Return ViewState("PhotoPathBase")
        End Get
    End Property

    Protected ReadOnly Property PhotoHeight() As String
        Get
            If ViewState("PhotoHeight") Is Nothing Then
                ViewState("PhotoHeight") = MyObj.GetParameter("WorkRecord", "PhotoHeight")
            End If

            Return ViewState("PhotoHeight")
        End Get
    End Property

    Protected ReadOnly Property PhotoWidth() As String
        Get
            If ViewState("PhotoWidth") Is Nothing Then
                ViewState("PhotoWidth") = MyObj.GetParameter("WorkRecord", "PhotoWidth")
            End If

            Return ViewState("PhotoWidth")
        End Get
    End Property

#End Region

End Class